﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphicsFramework.Support
{
    class MouseCollection
    {

        private List<Entity.Point2F> clicks = new List<Entity.Point2F>();

        public delegate void DrawingCompleteDelegate();
        public event DrawingCompleteDelegate DrawingComplete;

        private Enum.EnumMouseClicks required;

        private Mouse current;
        private Mouse down;
        private Mouse previous;
        private bool snap;

        public MouseCollection()
        {
            Snap = true;
        }

        public void Start(Enum.EnumMouseClicks click)
        {
            required = click;
            clicks.Clear();
        }

        public void Add(Entity.Point2F click)
        {
            Add(click.X, click.Y);
        }

        public void Add(float x, float y)
        {
            bool equal = false;

            foreach (Entity.Point2F click in clicks)
            {
                if (click.IsEqual(x, y))
                {
                    equal = true;
                    break;
                }
            }

            if (equal)
            {
                DrawingComplete();
            }
            else
            {
                clicks.Add(new Entity.Point2F(x, y));
                if (clicks.Count == (int)required)
                {
                    DrawingComplete();
                }
            }
        }

        public Entity.Point2F RemoveAndGetLast()
        {
            Entity.Point2F point = clicks[clicks.Count - 1].Clone();
            clicks.RemoveAt(clicks.Count - 1);
            return point;
        }

        public Mouse Current
        {
            get { return current; }
            set 
            { 
                current = value;
                current.Snap = snap;
            }
        }

        public Mouse Down
        {
            get { return down; }
            set 
            { 
                down = value;
                down.Snap = snap;
            }
        }

        public Mouse Previous
        {
            get { return previous; }
            set 
            {
                previous = value;
                previous.Snap = snap;
            }
        }

        public List<Entity.Point2F> Clicks
        {
            get { return clicks; }
            set { clicks = value; }
        }

        public bool Snap
        {
            get { return snap; }
            set { snap = value; }
        }

   }
}
